﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity;

using BO;
using BrightIdeasSoftware;
using UI.Core;
namespace UI.Security
{
    public partial class SysRoleForm :EditFormBase,IMenuForm
    {
        public SysRoleForm()
        {
            InitializeComponent();
            MyGrid = grid;
            MyGrid.SelectionChanged += grid_SelectionChanged;

        }
        void grid_SelectionChanged(object sender, EventArgs e)
        {
            gridUser.ClearObjects();
            var obj = grid.SelectedObject as SysRole;
            if (obj != null)
            {


                CurObj = obj;
                gridUser.Objects = CurObj.ISysUserRoleList;
            }


        }
        protected override object OnAdd()
        {
            var obj = new SysRole();
            return obj;

        }
     

        private void SysRoleForm_Load(object sender, EventArgs e)
        {
            var cols = new List<OLVColumn>();
            cols.Add(new OLVColumn("用户名", DataBaseObject.Columns.SysUserRole_UserName));          
            gridUser.Columns.AddRange(cols.ToArray());

      
            OnSetGridColumns();
            InitGrid();
            OnQuery();
            MyGrid.RowObjectType = typeof(SysRole);
        }
        SysRoleList mySource;
        protected override void OnQuery()
        {
         
            FetchData();
            DataBind();
        }
        protected override void FetchData()
        {
            mySource = ListFactory<SysRoleList>.GetList("");
             
            GridDataSource = mySource;
    
        }
        protected override void DataBind()
        {
            base.DataBind();
        
        }
        void OnSetGridColumns()
        {
            var cols = new List<OLVColumn>();
            cols.Add(new OLVColumn("角色", "RoleName") { Width = 250 });
            cols.Add(new OLVColumn("角色描述", "RoleDescription") { Width = 250 });
            //cols.Add(new OLVColumn("已停用", "IsInactive") { Width = 150
            //, AspectToStringConverter = new AspectToStringConverterDelegate(item => (bool)item == true ? "已停用" : "正常")
            //});

            MyGrid.Columns.AddRange(cols.ToArray());


        }

        protected override void OnSave()
        {
            try
            {
                foreach (SysRole delObj in RemovedList)
                {
                    if (delObj == null)
                        continue;
                     mySource.Remove(delObj);
                }
                RemovedList.Clear();

                foreach (SysRole obj in MyGrid.Objects)
                {
                    if (obj == null)
                        continue;
                    if (obj.IsChild == false)
                        mySource.Add(obj);

                }

           
                mySource.Save();
                OnQuery();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }

        }
        SysRole CurObj = null;
        private void btnAddObj_Click(object sender, EventArgs e)
        {
            if (CurObj == null)
                return;
           
                addUser();
         
         
        }

   
        void addUser()
        {
            var dform = new DataLookupForm<SysUserList>();
            if (dform.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var item in dform.ReturnObjects)
                {
                    var obj = item as SysUser;
                    if (obj != null)
                    {
                        if (CurObj.ISysUserRoleList.GetByOID(obj.UserName, CurObj.RoleName) == null)
                            CurObj.ISysUserRoleList.Add(new SysUserRole() { UserName = obj.UserName, RoleName = CurObj.RoleName });
                    }
                }
                gridUser.Objects = CurObj.ISysUserRoleList;
            }
        }

        private void btnRemoveObj_Click(object sender, EventArgs e)
        {
            if (CurObj == null)
                return;
                removeUser();
       
        }

     

        private void removeUser()
        {
            CurObj.ISysUserRoleList.Remove(gridUser.SelectedObject as SysUserRole);
            gridUser.Objects = CurObj.ISysUserRoleList;
        }
    }
}
